Introduction to R shiny package

Hakan Turgay & Bertan Taylan

Chapter 1

Motivation, Basic R-Shiny Package and Example

Motivation

  • Scientists mostly use R to process their analyzes

  • Presenting/Sharing their findings are usually done in static format

  • Problem : They cannot present additional questions directly

  • Simple idea: Immigrating ratios from a specific region of Turkey due to years and also forecasts about these ratios (e.g. Journalist)

What is Shiny package?

  • Shiny is an R package that makes it easy to build interactive web applications (apps) straight from R. This lesson will get you started building Shiny apps right away.
  • Build useful web applications with only a few lines of code—no JavaScript required.
  • Shiny applications are automatically “live” in the same way that spreadsheets are live. Outputs change instantly as users modify inputs, without requiring a reload of the browser.
  • Shiny user interfaces can be built entirely using R, or can be written directly in HTML, CSS, and JavaScript for more flexibility.
  • Pre-built output widgets for displaying plots, tables, and printed output of R objects.

Basically


R Shiny = R + interactivity + web made easy

kmeans

Install Shiny Package

If you still haven’t installed the Shiny package, open an R session, connect to the internet, and run

First App in Shiny

Code Output

firstapp

More Example

Chapter 2

Structure of a Shiny App

Main Structure

  • ui: Nested R functions that assemble an HTML user interface for the app

  • server : A function with instructions on how to build and rebuild the R objects displayed in the UI

  • shinyApp : Combines ui and server into a functioning app

  • Save the template as app.R

  • a call to the shinyApp function

Alternative Approach

kmeans

Say Hello with Shiny

Code Output

working code:

helloshiny

Chapter 3

Inputs and Outputs

Overview

Build your app around inputs and outputs

kmeans

Input Syntax

kmeans

Input

Code Output

input

Entegration Input and Output

Use 3 rules to write the server function

1 - Save objects to display to output$

2 - Build objects to display with render*()

[ING] UI tarafinda bulunan ‘hist’ (histogram) objesi server tarafindaki ‘RenderPlot’ methodu ile uretilecek . Not: hist ve RenderPlot vurgulanacak ..

Render Method

First Rule

3 - Access input values with input$

First Rule

…and more “Render Method” example

First Rule

Result (Reactivity)

Reactivity automatically occurs whenever you use an input value to render an output object

Code Output

working code:

res

Chapter 4

Using Checkbox in Shiny

Reviewing an Example

kmeans

Starting to Code Analysis

kmeans

Change code

Result

working code:

check

Chapter 5

Improving the Design

Design Part

  • Assemble UI with HTML/CSS/… widgets

  • Adjustment of the layout scheme

Chapter 6

Implementations in Shiny: JavaScript, HTML and CSS

Using HTML, CSS and Javascript in R Code


In R, HTML elements can be defined by tags keyword.

…and more Html Tag

html tag

External file import (html, css , js ext)

Method of importing depends on type of the file;

To include a CSS file


use includeCSS() or
1. Place the file in the www subdirectory
2. Link to it with:

To include JavaScript


use includeScript() or
1. Place the file in the www subdirectory
2. Link to it with:

To include HTML file

Layout structure of the UI

Combine multiple elements into a ”single element” that has its own properties with a panel function

Chapter 7

Case Studies: Predicted Deaths from Lung Disease

Using DyGraph Package in Shiny

Ui part of the code:

Server part of the code

Result

check

Chapter 8

Case Studies: Migration

Retrieving Data

Using Leaflet

  • For creating a map we used Leaflet library.
  • Leaflet is a open source JavaScript library for creating interactive maps.
  • It can be implemented and used in Shiny easily.
  • Leaflet gives the opportunity of adding informational popups to particular part of maps.
  • leaflet

Result

result